package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TestPaperReviewInfor
import kotlinx.android.synthetic.main.aibk_adapter_answersheet_child.view.*

/**
 * auther: 王国龙
 * Date：2020.06.15
 * describe:试卷预览对应的答题卡
 */
class AibkTestpaperReviewAnswerSheetChildAdapter(
        val activity: AppCompatActivity,
        val testPaperReviewInfor: TestPaperReviewInfor,
        var index_A: Int
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_answersheet_child, parent, false)
    }

    override fun onBindViewHolder(
            holder: KBaseRecyclerViewHolder,
            position: Int
    ) {
        val index_B = position_return_index_B(testPaperReviewInfor, position)
        val index_C = position_return_index_C(testPaperReviewInfor, position)

        val sortIndex = testPaperReviewInfor.TypeList[index_A].QuesList[index_B]
                .SortIndexList[index_C].SortIndex
        Log.e("onBindViewHolder", "==================sortIndex=================$sortIndex")
        holder.convertView.BtnSortIndex.text = sortIndex

        holder.convertView.BtnSortIndex.setOnClickListener {
            var index_C = position_return_index_C(testPaperReviewInfor, position)
            clickChilidListener!!.onClick(index_A, index_B, index_C)
        }

    }

    override fun getItemCount(): Int {
        var itemCount = 0
        var quesListSize = testPaperReviewInfor.TypeList[index_A].QuesList.size
        for (b in 0 until quesListSize) {
            itemCount += testPaperReviewInfor.TypeList[index_A].QuesList[b].SortIndexList.size
        }
        return itemCount
    }

    interface onClickChildListener {
        fun onClick(index_A: Int, index_B: Int, index_C: Int)
    }

    private var clickChilidListener: onClickChildListener? = null

    fun setClickChildListener(clickChilidListener: onClickChildListener?) {
        this.clickChilidListener = clickChilidListener
    }

    private fun position_return_index_B(testPaperReviewInfor: TestPaperReviewInfor, position: Int): Int {
        var postionX = -1
        var index_B = 0
        var QuesListSize = testPaperReviewInfor.TypeList[index_A].QuesList.size
        outLoop@ for (b in 0 until QuesListSize) {
            var SortIndexListSize = testPaperReviewInfor.TypeList[index_A].QuesList[b]
                    .SortIndexList.size
            for (c in 0 until SortIndexListSize) {
                postionX += 1
                if (position == postionX) {
                    index_B = b
                    break@outLoop
                }
            }
        }
        return index_B
    }

    private fun position_return_index_C(testPaperReviewInfor: TestPaperReviewInfor, position: Int): Int {
        var postionX = -1
        var index_C = 0
        var QuesListSize = testPaperReviewInfor.TypeList[index_A].QuesList.size
        outLoop@ for (b in 0 until QuesListSize) {
            var SortIndexListSize = testPaperReviewInfor.TypeList[index_A].QuesList[b]
                    .SortIndexList.size
            for (c in 0 until SortIndexListSize) {
                postionX += 1
                if (position == postionX) {
                    index_C = c
                    break@outLoop
                }
            }
        }
        return index_C
    }

}